import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

public class XMLUtil {
    public static Object getBean(String args){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("C:\\程序设计\\Java\\SpringHomework\\cross_platform_image_browsing_system\\src\\config.xml"));
            NodeList nodes = null;
            Node classNode = null;
            String cName = null;
            nodes = doc.getElementsByTagName("className");

            if(args.equals("image")){
                classNode = nodes.item(0).getFirstChild();
            } else if(args.equals("os")){
                classNode = nodes.item(1).getFirstChild();
            }
            cName = classNode.getNodeValue();
            Class c = Class.forName(cName);
            Object obj = c.newInstance();
            return obj;
        } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException |
                 InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
